﻿using System;
using System.IO;
using System.Reflection;
using System.Text;
using System.Web;
using VitCode.BaseDataPageCreator.Setting;

namespace AMS.Web
{
    public class Global : HttpApplication
    {

        protected void Application_Start(object sender, EventArgs e)
        {
            // 在应用程序启动时运行的代码

            //long totalCount = 0; //总访问数

            //int todayCount = 0;//今天在线人数

            ////先从XML中取得访问数

            //string xmlFilePath = AppDomain.CurrentDomain.BaseDirectory + "BackStageManagement/Count.xml"; //XML文件路径

            //DataSet ds = new DataSet();

            //ds.ReadXml(xmlFilePath);

            // totalCount = Int32.Parse(ds.Tables[0].Rows[0]["TotalCount"].ToString().Trim());
            // todayCount = Int32.Parse(ds.Tables[0].Rows[0]["TodayCount"].ToString().Trim());
            //设置Application，设置在线数量，总访问数量

            //Application["total_count"] = totalCount;
            //Application["today_count"] = todayCount;
            Application["online_count"] = 0;

            //XmlDocument xmlObj = new XmlDocument();

            //xmlObj.Load(xmlFilePath);

            //XmlNode xmlTotalOjb = xmlObj.SelectSingleNode("Count/TodayCount");

            //xmlTotalOjb.InnerText = 0.ToString();
        }

        protected void Session_Start(object sender, EventArgs e)
        {
            //string sessionId = Session.SessionID;  
             //在新会话启动时运行的代码

            Application.Lock();//锁定

            //long totalCount = 0; //总访问数

            //int todayCount = 0;//今天访问数

            //totalCount = Convert.ToInt32(Application["total_count"]);

            //todayCount = Convert.ToInt32(Application["today_count"]);

            //totalCount++;//用户登录，以后才修改访问总数

            //todayCount++;//在线用户数量加 1

            //Application["total_count"] = totalCount;

            //Application["today_count"] = todayCount;

            Application["online_count"] = (int) Application["online_count"] + 1;

            //把访问数写如到XML文件中

            //string xmlFilePath = AppDomain.CurrentDomain.BaseDirectory + "BackStageManagement/Count.xml"; //XML文件路径

            //XmlDocument xmlObj = new XmlDocument();

            //xmlObj.Load(xmlFilePath);

            //XmlNode xmlTotalOjb = xmlObj.SelectSingleNode("Count/TotalCount");

            //xmlTotalOjb.InnerText = totalCount.ToString();//增加总的访问量，并修改xml总访问量的值 

            //XmlNode xmlTodayNode = xmlObj.SelectSingleNode("Count/TodayCount");

            //xmlTodayNode.InnerText = todayCount.ToString();


            //xmlObj.Save(xmlFilePath);

            Application.UnLock(); //解锁
        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {

        }

        protected void Application_EndRequest(object sender, EventArgs e)
        {

        }


        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {
        }

        protected void Application_Error(object sender, EventArgs e)
        {
        }

        protected void Session_End(object sender, EventArgs e)
        {
            //这里主要计算登录用户

            Application.Lock();

            Application["online_count"] = (int) Application["online_count"] - 1;

            //  Application["today_count"] = Int32.Parse(Application["today_count"].ToString()) - 1;

            //当用户离开站点的时候,在线总数减1 

            Application.UnLock();
        }

        protected void Application_End(object sender, EventArgs e)
        {
        }
    }
}